﻿

--奖励相关接口
--[[
	awards = 
	{
		{type = 0, id = 2429, count = 1, quality = -1, strong = -1, bind = 1, param = 0},
		{type = 6, id = 0, count = 100},
	}
--]]


Awards =
{
	--检测背包空间 awards:奖励表 
	CheckBagGridCount = function (sysarg, awards, tips)
		if not awards then return false, 1000000 end
		local needCount = 0
		for k,v in pairs(awards) do
			if v.type == 0 or v.type == 52 or v.type == 53 or v.type == 54 then
				local quality = v.quality or 0
				local strong = v.strong or 0
				local bind = v.bind or 1
				local param = v.param or nil
				local guid = 0
				local count = math.floor(v.count)
				local id = v.id
				
				if v.type == 0 then
					if type(param) ~= 'number' and param ~= nil then
						id = Item.getItemProperty(sysarg, param, Item.ipItemID, 0)  --获得物品的ID 
					end
					
					if v.sex then
						local nSex = Actor.getIntProperty(sysarg,PROP_ACTOR_SEX)
						if nSex ~= v.sex and v.sex ~= -1 then
							count = 0
						end
					end
					if v.job then
						local nJob = Actor.getIntProperty(sysarg,PROP_ACTOR_VOCATION)
						if nJob ~= v.job and v.job ~= 0 then
							count = 0
						end
					end					
				elseif v.type == 52 then --跟据性别奖励物品
					-- 0:男；1:女
					local nSex = Actor.getIntProperty(sysarg,PROP_ACTOR_SEX)
					id = v.id[nSex + 1]
				elseif v.type == 53 then --跟据职业奖励物品
					-- 1:战士；2:法师；3:道士
					local nJob = Actor.getIntProperty(sysarg,PROP_ACTOR_VOCATION)
					id = v.id[nJob]
				elseif v.type == 54 then --跟据性别和职业奖励物品
					local nSex = Actor.getIntProperty(sysarg,PROP_ACTOR_SEX)
					local nJob = Actor.getIntProperty(sysarg,PROP_ACTOR_VOCATION)
					local itemids = v.id[nSex + 1]
					if itemids == nil then return false, 1000000 end
					
					id = itemids[nJob]
				end
				
				if id == nil or type(id) ~= 'number' then return false, 1000000 end					
				
				if count > 0 then
					needCount = needCount + Item.getAddItemNeedGridCount(sysarg, id, count, quality, strong, bind, guid)
				end
			end
		end
		
		if needCount <= 0 then return true, needCount end 
		
		local hasCount = Item.getBagEmptyGridCount(sysarg)
		if hasCount >= needCount then return true, needCount end
		
		-- 没有指定提示则显示默认提示
		if not tips then 
			Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.mt00001, needCount), ttFlyTip)			
		end
		return false, needCount
	end,
	
	CheckBagNeedGridCount = function (sysarg, needCount)
		if needCount <= 0 then return false end
		
		local hasCount = Item.getBagEmptyGridCount(sysarg)
		if hasCount < needCount then 
			Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.mt00001, needCount), ttFlyTip)
			return false
		end
		return true
	end,


	--给予奖励，调用前要先调用 Awards.CheckBagGridCount() awards:奖励表 logId:日志ID logStr:日志表述
	
	GiveNoCheck = function (sysarg, awards, logId, logStr, wayId)
		if not awards then return false end	
				
		local giveCount = 0;
		
		for k,v in pairs(awards) do
			local quality = v.quality or 0
			local strong = v.strong or 0
			local bind = v.bind or 1
			local param = v.param or nil
			local guid = 0
			wayId = wayId or -1
			local circle = v.circle or 0
			local sex = v.sex or -1
			local job = v.job or 0
			
			local id = v.id
			
			if v.type == 0 then
				if type(param) ~= 'number' and param ~= nil then
					id = Item.getItemProperty(sysarg, param, Item.ipItemID, 0)  --获得物品的ID 
				end
			elseif v.type == 52 then --跟据性别奖励物品
				-- 0:男；1:女
				local nSex = Actor.getIntProperty(sysarg,PROP_ACTOR_SEX)
				id = v.id[nSex + 1]
			elseif v.type == 53 then --跟据职业奖励物品
				-- 1:战士；2:法师；3:道士
				local nJob = Actor.getIntProperty(sysarg,PROP_ACTOR_VOCATION)
				id = v.id[nJob]
			elseif v.type == 54 then --跟据性别和职业奖励物品
				local nSex = Actor.getIntProperty(sysarg,PROP_ACTOR_SEX)
				local nJob = Actor.getIntProperty(sysarg,PROP_ACTOR_VOCATION)
				local itemids = v.id[nSex + 1]
				if itemids == nil then return false end
				
				id = itemids[nJob]
			end
			
			if id == nil or type(id) ~= 'number' then return false end
			
			local count = math.floor(v.count)
			if Actor.giveAward(sysarg, v.type, id, count, quality, strong, bind, guid, logId, logStr, wayId,circle,sex,job) ~= true then
				local actorId = Actor.getIntProperty(sysarg, PROP_ENTITY_ID)
				System.trace(string.format("Error:Give award error! actorId = %d, logId = %d, give type = %d, id = %d, count = %d, giveAwardCount = %d", actorId, logId, v.type, id, count, giveCount))
								
				--如果已经给了一部分奖励，避免玩家刷奖励，则返回 true，然后手工处理
				return (giveCount ~= 0) and true or false
			end
			
			giveCount = giveCount + 1			
			
		end		
				
		return true
	end,
	
	-- 给予奖励（包含了检查） awards:奖励表 logId:日志ID logStr:日志表述
	Give = function (sysarg, awards, logId, logStr, wayId)
		if not awards then return false end	
		if Awards.CheckBagGridCount(sysarg, awards) ~= true then return false end
		
		return Awards.GiveNoCheck(sysarg, awards, logId, logStr, wayId)
	end,

	--通过邮件给奖励，不需要检查背包，可发给不在线的玩家
	--nActorId = 0 发全服邮件
	GiveToMailById = function (nActorId, awards, title, content, logId, logStr, nActorJob, nActorSex)
		if not awards then 
			return false 
		end	
		if not nActorId then 
			return false 
		end
		if not logId then 
			logId = 0 
		end
		if not logStr then 
			logStr = "" 
		end
		
		local pAttach = System.GetAttachment()
		local nAttachCount = 0
		local bNeedSend = true
		for i=1, table.getn(awards) do
			local type = awards[i].type
			local id = awards[i].id
			local count = math.floor(awards[i].count)
			local bind = awards[i].bind or 1
			local quality = awards[i].quality or 0
			local strong = awards[i].strong or 0
			local sex = awards[i].sex or -1
			local job = awards[i].job or 0
			local time = awards[i].time or 0
			
			if nil == nActorJob or 0 == job or nActorJob == job then		--同职业
				if nil == nActorSex or -1 == sex or nActorSex == sex then	--同性别
					if type == 0 then
						--物品，发邮件
						if quality==0 and strong==0 and time==0 then
							System.AddAwardToAttachment(pAttach, type, id, count, bind)
						else
							local pItem = Item.createItem(id, count, strong, quality, bind, time)
							System.AddItemToAttachment(pAttach, pItem)
							Item.releaseItemPtr(pItem)
						end
						nAttachCount = nAttachCount + 1
						bNeedSend = true
					else
						System.AddAwardToAttachment(pAttach, type, id, count, bind)
						nAttachCount = nAttachCount + 1
						bNeedSend = true
					end

					if nAttachCount >=5 then
						--一封邮件最多5个附件
						System.SendEmailToActorById(nActorId, 0, title, content, pAttach, logId, logStr)
						pAttach = System.GetAttachment()	--清空附件内存
						nAttachCount= 0
						bNeedSend= false
					end
				end--end 同sex
			end	--end 同job
		end	--end for
		
		if bNeedSend then
			System.SendEmailToActorById(nActorId, 0, title, content, pAttach, logId, logStr)
		end
		return true
	end,
}

--[[
	消耗与奖励类型定义与项意义:
enum tagAwardType
{
	qatEquipment=0,             //物品或者装备 id:物品ID count:物品数量 quality:物品品质 strong:强化等级 bind:绑定状态 param:物品指针 
	qatExp = 1,                	//角色经验值 count:经验值 param:如果是任务，这个就填写任务的ID，其他的话填关键的有意义的参数，如果没有就填写0
	qatGuildContribution = 2,	//帮派贡献值 count:帮派贡献值
	qatBindMoney = 3,           //金币 count:金币值
	qatMoney = 4,	            //备用货币	count:备用货币
	qatBindYb = 5,	            //绑定元宝 count:绑定元宝
	qatTitle = 6,	            //称谓 count:称谓ID
	qatSkill = 7,	            //技能 count:技能ID param:技能等级
	qatAchievePoint =8,        //成就点 count:成就点
	qatActivity = 9,			//奖励活跃度 count:奖励活跃度
	qatYuanbao =10,              //元宝 count:元宝
	qatAddExp = 11,				//按经验配置表加经验 id:奖励库ID count:普通加成率 quality:vip加成率 加成率使用以1000为基数的整形 即n/1000
	qatCircleSoul = 12,			//转生值  转生修为
	qatBoss =13,                //boss
	qatUpgrade = 14,			//玩家升级
	qatPetUpgrade = 15,			//宝宝升级
	qatGuildCoin = 16,			//行会资金
	qatAwardHeroExp= 17,		//增加英雄的经验
	qatHeroUpgrade = 18,		//增加出战英雄等级
	qatInjectPower = 19,		//(手游在用)注灵，qatMagicPower->qatInjectPower
	qatHonorVaule = 20,			//荣誉值 功勋
	qatMeritVaule = 21,			//灵珠值
	qatSpiritShield = 22,		//神盾值
	qatFuSoulValue = 23,		//血符值
	qatDiamond = 24,			//武魂值
	qatRingCrystal = 25,		//特戒结晶
	qatLueiGongExp = 26,		//内功经验
	qatBossScore = 27,			//boss积分
	
	qatCustomize = 127,		    //自定义奖励

};

--]]
